{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次计算的结果为:sum=1\n",
      "第2次计算的结果为:sum=3\n",
      "第3次计算的结果为:sum=9\n",
      "第4次计算的结果为:sum=33\n",
      "第5次计算的结果为:sum=153\n",
      "第6次计算的结果为:sum=873\n",
      "第7次计算的结果为:sum=5913\n",
      "第8次计算的结果为:sum=46233\n",
      "第9次计算的结果为:sum=409113\n",
      "最终计算的结果为sum=409113\n"
     ]
    }
   ],
   "source": [
    "N = eval(input('请输入一个正整数'))\n",
    "i,sum,j,T=1,0,1,1\n",
    "while(i<=N):\n",
    "    while(j<=i):\n",
    "        T=T*j\n",
    "        j+=1\n",
    "    sum+=T\n",
    "    print(f'第{i}次计算的结果为:sum={sum}')\n",
    "    i+=1\n",
    "print(f'最终计算的结果为sum={sum}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ***基于我的理解，对程序进行的优化，将二次循环改进为一次循环***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次计算的结果为:sum=1\n",
      "第2次计算的结果为:sum=3\n",
      "第3次计算的结果为:sum=9\n",
      "第4次计算的结果为:sum=33\n",
      "第5次计算的结果为:sum=153\n",
      "第6次计算的结果为:sum=873\n",
      "第7次计算的结果为:sum=5913\n",
      "第8次计算的结果为:sum=46233\n",
      "第9次计算的结果为:sum=409113\n",
      "最终计算的结果为sum=409113\n"
     ]
    }
   ],
   "source": [
    "N = eval(input('请输入一个正整数'))\n",
    "i,sum,j=1,0,1\n",
    "for i in range(1,N+1): \n",
    "    j*=i\n",
    "    sum+=j\n",
    "    print(f'第{i}次计算的结果为:sum={sum}');\n",
    "print(f'最终计算的结果为sum={sum}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
